//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.alibaba.nacos.config.server.auth;

import com.alibaba.nacos.config.server.configuration.ConditionOnExternalStorage;
import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.model.User;
import com.alibaba.nacos.config.server.service.repository.PaginationHelper;
import com.alibaba.nacos.config.server.service.repository.RowMapperManager;
import com.alibaba.nacos.config.server.service.repository.extrnal.ExternalStoragePersistServiceImpl;
import com.alibaba.nacos.config.server.utils.LogUtil;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Conditional({ConditionOnExternalStorage.class})
@Component
public class ExternalUserPersistServiceImpl implements UserPersistService {
	@Autowired
	private ExternalStoragePersistServiceImpl persistService;
	private JdbcTemplate jt;

	public ExternalUserPersistServiceImpl() {
	}

	@PostConstruct
	protected void init() {
		this.jt = this.persistService.getJdbcTemplate();
	}

	public void createUser(String username, String password) {
		String sql = "INSERT INTO users (username, password, enabled) VALUES (?, ?, ?)";

		try {
			this.jt.update(sql, new Object[]{username, password, 1});
		} catch (CannotGetJdbcConnectionException var5) {
			LogUtil.FATAL_LOG.error("[db-error] " + var5.toString(), var5);
			throw var5;
		}
	}

	public void deleteUser(String username) {
		String sql = "DELETE FROM users WHERE username=?";

		try {
			this.jt.update(sql, new Object[]{username});
		} catch (CannotGetJdbcConnectionException var4) {
			LogUtil.FATAL_LOG.error("[db-error] " + var4.toString(), var4);
			throw var4;
		}
	}

	public void updateUserPassword(String username, String password) {
		try {
			this.jt.update("UPDATE users SET password = ? WHERE username=?", new Object[]{password, username});
		} catch (CannotGetJdbcConnectionException var4) {
			LogUtil.FATAL_LOG.error("[db-error] " + var4.toString(), var4);
			throw var4;
		}
	}

	public User findUserByUsername(String username) {
		String sql = "SELECT username,password FROM users WHERE username=? ";

		try {
			return (User)this.jt.queryForObject(sql, new Object[]{username}, RowMapperManager.USER_ROW_MAPPER);
		} catch (CannotGetJdbcConnectionException var4) {
			LogUtil.FATAL_LOG.error("[db-error] " + var4.toString(), var4);
			throw var4;
		} catch (EmptyResultDataAccessException var5) {
			return null;
		} catch (Exception var6) {
			LogUtil.FATAL_LOG.error("[db-other-error]" + var6.getMessage(), var6);
			throw new RuntimeException(var6);
		}
	}

	public Page<User> getUsers(int pageNo, int pageSize) {
		PaginationHelper<User> helper = this.persistService.createPaginationHelper();
		String sqlCountRows = "SELECT count(*) FROM users WHERE ";
		String sqlFetchRows = "SELECT username,password FROM users WHERE ";
		String where = " 1=1 ";

		try {
			Page<User> pageInfo = helper.fetchPage(sqlCountRows + where, sqlFetchRows + where, (new ArrayList()).toArray(), pageNo, pageSize, RowMapperManager.USER_ROW_MAPPER);
			if (pageInfo == null) {
				pageInfo = new Page();
				pageInfo.setTotalCount(0);
				pageInfo.setPageItems(new ArrayList());
			}

			return pageInfo;
		} catch (CannotGetJdbcConnectionException var8) {
			LogUtil.FATAL_LOG.error("[db-error] " + var8.toString(), var8);
			throw var8;
		}
	}

	public List<String> findUserLikeUsername(String username) {
		String sql = "SELECT username FROM users WHERE username LIKE '%"+username+"%'";
		List<String> users = this.jt.queryForList(sql, String.class);
		return users;
	}
}
